#!/bin/bash

# Assemble the GreptimeDB binary download URL for a specific version.
binary_url() {
    local ver="$1"
    local bin_tar="greptime-$(uname -s | tr '[:upper:]' '[:lower:]')-amd64-v$ver.tar.gz"
    echo "https://github.com/GreptimeTeam/greptimedb/releases/download/v$ver/$bin_tar"
}

# Download a specific version of GreptimeDB binary tar file, untar it to folder `./bins/$ver`.
# `ver` is semver without prefix `v`
download_binary() {
    local ver="$1"
    local url="$(binary_url $ver)"
    local bin_tar="greptime-$(uname -s | tr '[:upper:]' '[:lower:]')-amd64-v$ver.tar.gz"

    if [ -f ./bins/$ver/greptime ]; then
        echo " === binaries exist: $(ls ./bins/$ver/* | tr '\n' ' ')"
        chmod +x ./bins/$ver/*
        return
    fi

    if [ -f "$bin_tar" ]; then
        echo " === tar file exists: $bin_tar"
    else
        echo " === Download binary ver: $ver"
        echo " === Download binary url: $url"
        curl --connect-timeout 5 --retry 5 --retry-delay 1 -L "$url" -o "$bin_tar"
    fi

    mkdir -p ./bins/$ver
    tar -xf "$bin_tar" --strip-components=1 -C ./bins/$ver

    echo " === unpacked: ./bins/$ver:"
    ls -lh ./bins/$ver

    chmod +x ./bins/$ver/*
}

# Test data compatibility that:
# - the data written by an old version of GreptimeDB can be read by the current one
# - the data written by the current version of GreptimeDB can be read by an old one ("forward" compatibility)
run_test() {
    local old_ver="$1"
    local forward="$2"

    local write_case_dir="./tests/compat/case/write"
    local read_case_dir="./tests/compat/case/read"
    local bin_old="./bins/$old_ver/greptime"
    local bin_new="./bins/current/greptime"
    local runner="./bins/current/sqlness-runner"

    echo " === Test with:"
    echo " === old greptimedb version:"
    "$bin_old" --version
    echo " === new greptimedb version:"
    "$bin_new" --version

    # "forward" means we are testing forward compatibility:
    # the data generated by current version GreptimeDB can be used by old.
    # So we run new GreptimeDB binary first to write, then run old to read.
    # And the opposite for backward compatibility.
    if [ "$forward" == "forward" ]
    then
        echo " === Running forward compat test ..."
        echo " === Run test: write with current GreptimeDB"
        $runner --bins-dir $(dirname $bin_new) --case-dir $write_case_dir
    else
        echo " === Running backward compat test ..."
        echo " === Run test: write with old GreptimeDB"
        $runner --bins-dir $(dirname $bin_old) --case-dir $write_case_dir
    fi

    if [ "$forward" == 'forward' ]
    then
        echo " === Run test: read with old GreptimeDB"
        $runner --bins-dir $(dirname $bin_old) --case-dir $read_case_dir
    else
        echo " === Run test: read with current GreptimeDB"
        $runner --bins-dir $(dirname $bin_new) --case-dir $read_case_dir
    fi
}
